package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.*;
import cn.wolfcode.wolf2w.search.vo.SearchDateVo;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ISearchService searchService;

    @Autowired
    private IDestinationEsService destinationEsService;
    @Autowired
    private ITravelEsService travelEsService;
    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private IStrategyEsService strategyEsService;

    @GetMapping("/q")
    public Object SearchDate(SearchQueryObject qo) throws UnsupportedEncodingException {
        //中文解码
        String kw = URLDecoder.decode(qo.getKeyword(),"UTF-8");
        qo.setKeyword(kw);

        switch (qo.getType()){
            case SearchQueryObject.TYPE_DEST : return searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY : return searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL : return searchTravel(qo);
            case SearchQueryObject.TYPE_USERINFO : return searchUserInfo(qo);
            default:return searchAll(qo);
        }
    }

    /**
     * 查全部,全文检索
     * @param qo
     * @return
     */
    private Object searchAll(SearchQueryObject qo) {
        SearchDateVo vo = new SearchDateVo();
        long total = 0L;
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME,UserInfoEs.TYPE_NAME,
                UserInfo.class,qo,"info","city");
        total = total + us.getTotalElements() ;
        vo.setUsers(us.getContent());
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME,TravelEs.TYPE_NAME,
                Travel.class,qo,"title","summary");
        total = total + ts.getTotalElements() ;
        vo.setTravels(ts.getContent());
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME,StrategyEs.TYPE_NAME,
                Strategy.class,qo,"title","subTitle","summary");
        total = total + sts.getTotalElements() ;
        vo.setStrategys(sts.getContent());
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME,DestinationEs.TYPE_NAME,
                Destination.class,qo,"name","info");
        total = total + ds.getTotalElements() ;
        vo.setDests(ds.getContent());
        vo.setTotal(total);
        Map<String, Object> map = new HashMap<>();
        map.put("dest",ds);
        map.put("result",vo);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    /**
     * 查用户,全文检索
     * @param qo
     * @return
     */
    private Object searchUserInfo(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME,UserInfoEs.TYPE_NAME,
                UserInfo.class,qo,"info","city");
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    /**
     * 查游记,全文检索
     * @param qo
     * @return
     */
    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME,TravelEs.TYPE_NAME,
                Travel.class,qo,"title","summary");
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    /**
     * 查攻略,全文检索
     * @param qo
     * @return
     */
    private Object searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME,StrategyEs.TYPE_NAME,
                Strategy.class,qo,"title","subTitle","summary");
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    /**
     * 查目的地,精确检索
     * @param qo
     * @return
     */
    private Object searchDest(SearchQueryObject qo) {

        //目的地
        SearchDateVo vo = new SearchDateVo();
        Destination dest = destinationService.queryByName(qo.getKeyword());
        if(dest != null){
            //攻略
            List<Strategy> strategyList = strategyService.queryByDestName(qo.getKeyword());
            vo.setStrategys(strategyList);
            //游记
            List<Travel> travelList = travelService.queryByDestName(qo.getKeyword());
            vo.setTravels(travelList);
            //用户
            List<UserInfo> userInfoList = userInfoService.queryByCity(qo.getKeyword());
            vo.setUsers(userInfoList);
            long total = strategyList.size()+travelList.size()+userInfoList.size();
            vo.setTotal(total);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dest",dest);
        map.put("result",vo);
        map.put("qo",qo);
        return JsonResult.success(map);
    }
}
